<template>
  <a-row id="globalHeader" align="center" :wrap="false">
    <a-col flex="auto">
      <a-menu
        mode="horizontal"
        :selected-keys="selectedKeys"
        @menu-item-click="doMenuClick"
      >
        <a-menu-item
          key="0"
          :style="{ padding: 0, marginRight: '38px' }"
          disabled
        >
          <div class="titleBar">
            <img class="logo" src="../assets/logo.png" />
            <div class="title">煤矿企业安全测评系统</div>
          </div>
        </a-menu-item>
        <a-menu-item v-for="item in visibleRoutes" :key="item.path">
          {{ item.name }}
        </a-menu-item>
      </a-menu>
    </a-col>
    <a-col flex="100px">
      <!--      <div v-if="loginUserStore.loginUser.id">-->
      <!--        <a-dropdown>-->
      <!--          <a-avatar>{{ loginUserStore.loginUser.userName ?? "无名" }}</a-avatar>-->
      <!--          <template #content>-->
      <!--            <a-doption @click="userLogoutUsingPost">注销</a-doption>-->
      <!--          </template>-->
      <!--        </a-dropdown>-->
      <!--      </div>-->
      <div v-if="loginUserStore.loginUser.id">
        <a-dropdown>
          <a-avatar class="custom-button">{{
            loginUserStore.loginUser.userName ?? "无名"
          }}</a-avatar>
          <template #content>
            <a-doption @click="userLogoutUsingPost">注销</a-doption>
          </template>
        </a-dropdown>
      </div>
      <div v-else>
        <a-button type="primary" href="/user/login">登录</a-button>
      </div>
    </a-col>
  </a-row>
</template>

<script setup lang="ts">
import { routes } from "@/router/routes";
import { useRouter } from "vue-router";
import { computed, ref } from "vue";
import { useLoginUserStore } from "@/store/userStore";
import message from "@arco-design/web-vue/es/message";
import checkAccess from "@/access/checkAccess";
import { userLogoutUsingPost } from "@/api/userController";

const loginUserStore = useLoginUserStore();

const router = useRouter();
// 当前选中的菜单项
const selectedKeys = ref(["/"]);
// 路由跳转时，自动更新选中的菜单项
router.afterEach((to, from, failure) => {
  selectedKeys.value = [to.path];
});

// 展示在菜单栏的路由数组
const visibleRoutes = computed(() => {
  return routes.filter((item) => {
    if (item.meta?.hideInMenu) {
      return false;
    }
    // 根据权限过滤菜单
    if (!checkAccess(loginUserStore.loginUser, item.meta?.access as string)) {
      return false;
    }
    return true;
  });
});

// 点击菜单跳转到对应页面
const doMenuClick = (key: string) => {
  router.push({
    path: key,
  });
};

/**
 * 注销
 */
const handleLogout = async () => {
  try {
    // 调用后端的注销接口
    const res = await userLogoutUsingPost();
    // 检查响应状态码
    if (res.data.code === 0) {
      // 注销成功
      console.log("Logout successful");
      // 清除本地登录状态
      // loginUserStore.setLogout();
      // 跳转到登录页面
      router.push({
        path: "/user/login",
        replace: true,
      });
      // 提示用户注销成功
      message.success("注销成功");
    } else {
      // 注销失败，显示错误信息
      message.error("注销失败，" + res.data.message);
    }
  } catch (error) {
    // 捕获网络请求或其他异常错误
    console.error("Logout request failed", error);
    message.error("注销请求失败，请稍后再试");
  }
};
</script>

<style scoped>
#globalHeader {
}

.titleBar {
  display: flex;
  align-items: center;
}

.title {
  margin-left: 16px;
  color: black;
}

.logo {
  height: 48px;
}

.custom-button {
  background-color: #1a4fbc; /* 蓝色背景 */
  color: #fff; /* 白色字体 */
  //border: none; /* 无边框 */
  //border-radius: 0; /* 无圆角 */
  padding: 8px 16px; /* 内边距 */
  font-size: 14px; /* 字体大小 */
  cursor: pointer; /* 鼠标悬停时显示手型 */
}

.custom-button:hover {
  background-color: #1a4fbc; /* 鼠标悬停时的背景色 */
}
</style>
